﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WebServer.Models;

namespace WebServer
{
    /// <summary>
    /// Summary description for WebAPI
    /// </summary>
    [WebService(Namespace = "http://www.madis.dk/Uni/FlexiBus", Name = "WebAPI")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebAPI : System.Web.Services.WebService
    {

        [WebMethod]
        public String[] GetDropOffPoints()
        {
            using (StorageConnectorDataContext context = new StorageConnectorDataContext())
            {

                var dropOffs = from d in context.DropOffs select d.Name;
                return dropOffs.ToArray();
            }
        }

        [WebMethod]
        public Int32 RequestWithFixedDropOffTime(Transport transport)
        {
            using (StorageConnectorDataContext context = new StorageConnectorDataContext())
            {
                if (context.DropOffs.Where(d => d.Name.Equals(transport.DropOffLocation)).Count() == 0)
                {
                    return 0;
                }
                context.Transports.InsertOnSubmit(transport);
                try
                {
                    context.SubmitChanges();
                    if (transport.ID != 0)
                    {
                        return transport.ID;
                    }

                }
                catch (Exception e)
                {
                    return 0;
                }
            }


            return 0;
        }

        [WebMethod]
        public TransportSuggestion GetPickupInformation(Int32 transportID)
        {
            using (StorageConnectorDataContext context = new StorageConnectorDataContext())
            {
                TransportSuggestion sugg = new TransportSuggestion()
                {
                    ErrorCode = TransportSuggestion.ErrorCodes.None,
                    TransportID = transportID
                };

                try
                {
                    //Ikke behandlet endnu = 0
                    var transport = context.Transports.Single(t => t.ID == transportID);
                    var waypoint = context.Waypoints.Single(w => w.RouteID == transport.RouteID && w.Address.Equals(transport.PickupAdress));
                    sugg.PickUp = waypoint.EstimatedArrival;
                    //Rejected, ikke plads eller whatever
                }
                catch (Exception exce)
                {
                    sugg.ErrorCode = TransportSuggestion.ErrorCodes.NotFound;
                }

                return sugg;
            }

        }

        [WebMethod]
        public Boolean CancelRequest(int transportID)
        {
            using (StorageConnectorDataContext context = new StorageConnectorDataContext())
            {
                try
                {
                    var transport = context.Transports.Single(t => t.ID == transportID);

                    if (transport != null)
                    {
                        Int32 routeID = 0;
                        if (transport.RouteID != null)
                        {
                            routeID = (Int32)transport.RouteID;
                        }
                        context.Transports.DeleteOnSubmit(transport);
                        context.SubmitChanges();

                        if (routeID != 0)
                        {
                            RouteCalculator calc = new RouteCalculator();
                            routeID = calc.CalculateUpdatedDirections(routeID);

                            if (routeID == 0)
                            {
                                throw new Exception("Error happened.");
                            }
                        }

                    }

                }
                catch (Exception exc)
                {
                    return false;
                }

                return true;
            }
        }

        [WebMethod]
        public List<Transport> GetTransports_DropoffBetweenDateTimes(DateTime from, DateTime to)
        {
            using (StorageConnectorDataContext context = new StorageConnectorDataContext())
            {
                return context.Transports.Where(t => from <= t.DropOffTime && t.DropOffTime <= to).OrderBy(t => t.DropOffTime).ToList();
            }
        }



    }
}
